//
//  XDShakeViewController.m
//  Le way walkers
//
//  Created by POP-mac on 15/5/13.
//  Copyright (c) 2015年 LTXZ. All rights reserved.
//

#import "XDShakeViewController.h"
#import "XDShakeView.h"
#import "XDPhotoTextViewController.h"
#import "NetworkHandle.h"
#import "NNetworkHandle.h"
#import "UIImageView+WebCache.h"
#import "XDShakeSearchViewController.h"
#import "XDPlaceScenicDetailsViewController.h"
@interface XDShakeViewController ()<UISearchBarDelegate>
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UIImageView *imageView_1;
@property (nonatomic, retain) UIImageView *ticket_image;
@property (nonatomic, retain) UIVisualEffectView *vev;
@property (nonatomic, retain) UIActivityIndicatorView *testActivityIndicator;
@property (nonatomic, retain) NSMutableDictionary *dic_place;
@property (nonatomic, retain) NSMutableDictionary *dic_place_1;
@property (nonatomic, retain) UIView *photo_back;
@property (nonatomic, retain) UILabel *name_place;
@property (nonatomic, assign) BOOL country;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic, assign) BOOL panduan;
@end

@implementation XDShakeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    XDShakeView *view1 = [[XDShakeView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//    
//    [self.view addSubview:view1];
//    
//    [view1 release];
    self.panduan = YES;
    
    self.search = [[[UISearchBar alloc] init] autorelease];
    [self.search setFrame:CGRectMake(20, 10, self.view.frame.size.width - 40, 20)];
//    [search setBarStyle:UIBarStyleBlackTranslucent];
    [self.search setSearchBarStyle:UISearchBarStyleDefault];
    [self.search setKeyboardAppearance:UIKeyboardAppearanceDark];
    [self.search setKeyboardType:UIKeyboardTypeNamePhonePad];
    self.search.delegate = self;
    [self.navigationController.navigationBar addSubview:self.search];
    
    [self.view setBackgroundColor:[UIColor blackColor]];
    UIImage *image = [UIImage imageNamed:@"摇一摇背景图上"];
    self.imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
    self.imageView.alpha = 1;
    [self.imageView setFrame:CGRectMake(0, -49, self.view.frame.size.width, self.view.frame.size.height / 2.0)];
    [self.view addSubview:self.imageView];
    
    UIImage *image_1 = [UIImage imageNamed:@"摇一摇背景图下"];
    self.imageView_1 = [[[UIImageView alloc] initWithImage:image_1] autorelease];
    self.imageView_1.alpha = 1;
    [self.imageView_1 setFrame:CGRectMake(0, self.view.frame.size.height / 2.0 - 49, self.view.frame.size.width, self.view.frame.size.height / 2.0)];
    [self.view addSubview:self.imageView_1];
    
    UIBlurEffect *blu = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    self.vev = [[UIVisualEffectView alloc] initWithEffect:blu];
    self.vev.alpha = 0;
    [self.vev setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:self.vev];
    
    self.testActivityIndicator =[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    self.testActivityIndicator.tag = 20000;
    self.testActivityIndicator.hidden = YES;
    self.testActivityIndicator.center = CGPointMake(self.view.frame.size.width / 2, 120);//只能设置中心，不能设置大小
    // [testActivityIndicator setFrame = CGRectMack(100, 100, 100, 100)];不建议这样设置，因为UIActivityIndicatorView是不能改变大小只能改变位置，这样设置得到的结果是控件的中心在（100，100）上，而不是和其他控件的frame一样左上角在（100， 100）长为100，宽为100.
    [self.imageView_1 addSubview:self.testActivityIndicator];
    self.testActivityIndicator.color = [UIColor blueColor]; // 改变圈圈的颜色为红色； iOS5引入
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    self.search.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"%@", self.search.text);
    XDShakeSearchViewController *xd = [[XDShakeSearchViewController alloc] init];
    xd.search_text = self.search.text;
    [self.search resignFirstResponder];
    self.search.hidden = YES;
    [self.navigationController pushViewController:xd animated:YES];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.search resignFirstResponder];
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    
    CAKeyframeAnimation *key = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    CGMutablePathRef path = CGPathCreateMutable();
    CGPoint o = self.imageView.center;
    CGPathMoveToPoint(path, NULL, self.imageView.center.x, self.imageView.center.y);
    CGPathAddLineToPoint(path, NULL, o.x, o.y - 100);
    CGPathAddLineToPoint(path, NULL, o.x, o.y);
    [key setSpeed:2];
    [key setPath:path];
    [key setDuration:2.f];
    [self.imageView.layer addAnimation:key forKey:@"ss"];
    
    
    CAKeyframeAnimation *key_1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    CGMutablePathRef path_1 = CGPathCreateMutable();
    CGPoint o_1 = self.imageView_1.center;
    CGPathMoveToPoint(path_1, NULL, self.imageView_1.center.x, self.imageView_1.center.y);
    CGPathAddLineToPoint(path_1, NULL, o_1.x, o_1.y + 100);
    CGPathAddLineToPoint(path_1, NULL, o_1.x, o_1.y);
    [key_1 setSpeed:2];
    [key_1 setPath:path_1];
    [key_1 setDuration:2.f];
    [self.imageView_1.layer addAnimation:key_1 forKey:@"ss_1"];
    NSLog(@"%f %f", o_1.x, o_1.y);
    
    self.testActivityIndicator.hidden = NO;
    [self.testActivityIndicator startAnimating]; // 开始旋转
    [self.testActivityIndicator setHidesWhenStopped:YES]; //当旋转结束时隐藏
    
    self.vev.alpha = 0;
    
    [self.ticket_image removeFromSuperview];
    [self.photo_back removeFromSuperview];
    [self.name_place removeFromSuperview];
    
    
    
    NSLog(@"开始");
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"晃动取消");
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (!self.panduan) {
        return;
    }
    self.panduan = NO;
    self.testActivityIndicator.hidden = NO;
    if (self.country == YES) {
        //判断动画是否隐藏
        [self.testActivityIndicator stopAnimating];
        [self.testActivityIndicator hidesWhenStopped];
        self.testActivityIndicator.hidden = YES;
        return;
        
    }
    self.dic_place = [NSMutableDictionary dictionary];
    self.dic_place_1 = [NSMutableDictionary dictionary];
    self.num = arc4random() % 2;
    NSLog(@"%ld", self.num);
    self.country = YES;
    [NetworkHandle getDataWithURL:@"http://api.breadtrip.com/v5/index/" completion:^(id result) {
        NSArray *arr = [[[result objectForKey:@"search_data"] objectAtIndex:self.num] objectForKey:@"elements"];
        NSInteger num_1 = arc4random() % (arr.count);
        NSString *url = [[arr objectAtIndex:num_1] objectForKey:@"url"];
        NSLog(@"%@", [url substringFromIndex:7]);
        
        [NNetworkHandle getDataWithUrl:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place%@", [url substringFromIndex:7]] Cookie:@"sessionid=256c546b7892e31bd78f3cd9d9c200bd; bt_devid=a_8f22e38eeda805d03f5ceacc53bdea86841d9a13218d2015edc1a3c46e41fa62; refer_android_source=105" completion:^(id result) {
            self.dic_place_1 = [arr objectAtIndex:num_1];
            NSArray *arr_1 = [result objectForKey:@"hottest_sites"];
            self.dic_place = [arr_1 objectAtIndex:(arc4random() % arr_1.count)];
            NSLog(@"%@", [self.dic_place objectForKey:@"name"]);
            [self ticket];
            [self.testActivityIndicator stopAnimating];
            self.testActivityIndicator.hidesWhenStopped = YES;
            self.testActivityIndicator.hidden = YES;
            self.country = NO;
            self.panduan = YES;
        }];
        
    }];
    
    NSLog(@"晃动结束");
}

- (void)ticket
{
//    self.vev.alpha = 0.9;
    [self.photo_back removeFromSuperview];
    [self.ticket_image removeFromSuperview];
    [self.name_place removeFromSuperview];
    //背景
    self.photo_back = [[[UIView alloc] init] autorelease];
    [self.photo_back setFrame:CGRectMake(50, self.view.frame.size.height - 130, self.view.frame.size.width - 100, 80)];
    [self.photo_back.layer setCornerRadius:10];
    [self.photo_back setBackgroundColor:[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.4]];
    [self.view addSubview:self.photo_back];
    
    //人机交互
    self.photo_back.userInteractionEnabled = YES;
    
    //方法
    UITapGestureRecognizer *tap_image = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(image_tap)] autorelease];
    [self.photo_back addGestureRecognizer:tap_image];
    
    //照片
    self.ticket_image = [[[UIImageView alloc] init] autorelease];
    [self.ticket_image sd_setImageWithURL:[NSURL URLWithString:[self.dic_place objectForKey:@"cover_s"]] placeholderImage:[UIImage imageNamed:@"icon"]];
    [self.ticket_image setFrame:CGRectMake(10, 10, 60, 60)];
    [self.photo_back addSubview:self.ticket_image];
    
    //文字
    self.name_place = [[[UILabel alloc] init] autorelease];
    if (self.num == 1) {
        //中国景点 用 dic_place_1
        self.name_place.text = [self.dic_place_1 objectForKey:@"name"];
    }
    else
    {
        //外国景点 用 dic_place
        self.name_place.text = [self.dic_place objectForKey:@"name"];
    }
    self.name_place.textColor = [UIColor whiteColor];
    [self.name_place setFrame:CGRectMake(self.ticket_image.frame.origin.x + 10 + 60, self.ticket_image.frame.origin.y + 5, self.photo_back.frame.size.width - 20 - 60 - 10, 20)];
    [self.photo_back addSubview:self.name_place];
    
}

- (void)image_tap
{
    NSLog(@"hah");
    
    //点击事件判断 self.num == 0是外国 == 1 是中国 调用不同的数组self.dic_place和self.dic_place_1
    if (self.num == 0) {
        XDPlaceScenicDetailsViewController *xd = [[XDPlaceScenicDetailsViewController alloc] init];
        xd.typeName = [self.dic_place objectForKey:@"type"];
        xd.ScenicID = [self.dic_place objectForKey:@"id"];
        self.search.hidden = YES;
        [self.navigationController pushViewController:xd animated:YES];
    }
    else
    {
        XDPlaceScenicDetailsViewController *xd = [[XDPlaceScenicDetailsViewController alloc] init];
        xd.typeName = [self.dic_place_1 objectForKey:@"type"];
        xd.ScenicID = [self.dic_place_1 objectForKey:@"id"];
        self.search.hidden = YES;
        [self.navigationController pushViewController:xd animated:YES];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
